package everything;

public class Automaton extends Agent {

	public Automaton(String name, String color, int x, int y, WorldMap map, String direction) {
		super(name, color, x, y, map);
		_orientation = direction;
		_type = "automaton";
	}

	private String opposingDirection(String direction){
		if (direction.equalsIgnoreCase("NORTH")) return "SOUTH";
		else if (direction.equalsIgnoreCase("SOUTH")) return "NORTH";
		else if (direction.equalsIgnoreCase("EAST")) return "WEST";
		else if (direction.equalsIgnoreCase("WEST")) return "EAST";
		else return null;
	}

	@Override
	public void makeMove() {
		// kill agents in range
		_map.tryToKill(this);

		if ((_orientation.equalsIgnoreCase("NORTH") || _orientation.equalsIgnoreCase("SOUTH")) && _map.isMovementBlocked(_xLocation - 1, _yLocation) && _map.isMovementBlocked(_xLocation + 1, _yLocation)){
			//cant move
		} else if ((_orientation.equalsIgnoreCase("WEST") || _orientation.equalsIgnoreCase("EAST")) && _map.isMovementBlocked(_xLocation, _yLocation + 1) && _map.isMovementBlocked(_xLocation, _yLocation - 1)){
			//cant move
		} else if (_orientation.equalsIgnoreCase("NORTH") && _map.isMovementBlocked(_xLocation - 1, _yLocation))
			move(opposingDirection(_orientation));
		else if(_orientation.equalsIgnoreCase("SOUTH") && _map.isMovementBlocked(_xLocation + 1, _yLocation))
			move(opposingDirection(_orientation));
		else if(_orientation.equalsIgnoreCase("WEST") && _map.isMovementBlocked(_xLocation , _yLocation - 1))
			move(opposingDirection(_orientation));
		else if(_orientation.equalsIgnoreCase("EAST") && _map.isMovementBlocked(_xLocation , _yLocation + 1))
			move(opposingDirection(_orientation));
		else move(_orientation);
	}

	@Override
	// returns if there is another agent in its firing range
	public boolean isKilling(int x, int y, WorldMap map) {
		// other agent is on same row
		if (x == _xLocation){
			if (y > _yLocation){
				// if there is a wall in the way
				for (int i = _yLocation; i<y ; i++){
					if (map.isFireBlocked(x,i)){
						return false;
					}
				}
				return true;
			} else if (y < _yLocation){
				for (int i = y; i<_yLocation ; i++){
					if (map.isFireBlocked(x,i)){
						return false;
					}
				}
				return true;
			} else return false;
		}
		// other agent is on same column
		else if (y == _yLocation){
			if (x > _xLocation){
				for (int i = _xLocation; i<x ; i++){
					if (map.isFireBlocked(i,y)){
						return false;
					}
				}
				return true;
			} else if (x < _xLocation){
				for (int i = x; i<_xLocation ; i++){
					if (map.isFireBlocked(i,y)){
						return false;
					}
				}
				return true;
			} else return false;
		} else return false;
	}

	// returns if there is another agent in its firing range
	public static boolean isKilling(int _xLocation, int _yLocation, int x, int y, WorldMap map) {
		// other agent is on same row
		if (x == _xLocation){
			if (y > _yLocation){
				// if there is a wall in the way
				for (int i = _yLocation; i<y ; i++){
					if (map.isFireBlocked(x,i)){
						return false;
					}
				}
				return true;
			} else if (y < _yLocation){
				for (int i = y; i<_yLocation ; i++){
					if (map.isFireBlocked(x,i)){
						return false;
					}
				}
				return true;
			} else return false;
		}
		// other agent is on same column
		else if (y == _yLocation){
			if (x > _xLocation){
				for (int i = _xLocation; i<x ; i++){
					if (map.isFireBlocked(i,y)){
						return false;
					}
				}
				return true;
			} else if (x < _xLocation){
				for (int i = x; i<_xLocation ; i++){
					if (map.isFireBlocked(i,y)){
						return false;
					}
				}
				return true;
			} else return false;
		} else return false;
	}


}
